/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Pat P. <patgus@stonewwwall.org> ,               *
 *                         Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;

/** This class had been written by Pat P. and myself. It handles every *secure* 
    operations used in JMail */
final class JMailSecurity
{
    static String hashAndEncode(String str)
    {
        MessageDigest md = null;

        try
        {
            md = MessageDigest.getInstance("SHA1");
        }

        catch (NoSuchAlgorithmException noSuchAlgoEx)
        {
            noSuchAlgoEx.printStackTrace();
            return (null);
        }

        md.update(str.getBytes());
        byte[] hashed = md.digest();

        BASE64Encoder encoder = new BASE64Encoder();
        return (encoder.encode(hashed));
    }

    static String cryptAndEncode(String key, String str)
    {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "BlowFish");
        Cipher cipher;

        try
        {
            cipher = Cipher.getInstance("BlowFish");
        }

        catch (NoSuchAlgorithmException noSuchAlgoEx)
        {
            noSuchAlgoEx.printStackTrace();
            return (null);
        }

        catch (NoSuchPaddingException noSuchPaddingEx)
        {
            noSuchPaddingEx.printStackTrace();
            return (null);
        }

        try
        {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        }

        catch (InvalidKeyException invalidKeyEx)
        {
            invalidKeyEx.printStackTrace();
            return (null);
        }

        byte[] encrypted;

        try
        {
            encrypted = cipher.doFinal(str.getBytes());
        }

        catch (IllegalBlockSizeException illegalBlockSizeEx)
        {
            illegalBlockSizeEx.printStackTrace();
            return (null);
        }

        catch (BadPaddingException badPaddingEx)
        {
            badPaddingEx.printStackTrace();
            return (null);
        }

        BASE64Encoder encoder = new BASE64Encoder();

        return (encoder.encode(encrypted));
    }

    static String decodeAndDecrypt(String key, String str)
    {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "Blowfish");

        Cipher cipher = null;

        try
        {
            cipher = Cipher.getInstance("Blowfish");
        }

        catch (NoSuchAlgorithmException noSuchAlgoEx)
        {
            noSuchAlgoEx.printStackTrace();
            return (null);
        }

        catch (NoSuchPaddingException noSuchPaddingEx)
        {
            noSuchPaddingEx.printStackTrace();
            return (null);
        }

        try
        {
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
        }

        catch (InvalidKeyException invalidKeyEx)
        {
            invalidKeyEx.printStackTrace();
            return (null);
        }

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decrypted;

        try
        {
            decrypted = cipher.doFinal(decoder.decodeBuffer(str));
        }

        catch (IOException ioEx)
        {
            ioEx.printStackTrace();
            return (null);
        }

        catch (IllegalBlockSizeException illegalBlockSizeEx)
        {
            illegalBlockSizeEx.printStackTrace();
            return (null);
        }

        catch (BadPaddingException badPaddingEx)
        {
            badPaddingEx.printStackTrace();
            return (null);
        }

        return (new String(decrypted));
    }
}
